﻿using Project.API.Models;
using Project.BLL;
using Project.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Objects;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Project.API.Controllers
{
    public class DDmanageController : ApiController
    {
        Entities model = new Entities();

        // GET: api/DDmanage
        [Route("Api/DDmanage/Porder")]
        public ResultPageList<DDmanageDto> Get(int page, int limit, int sid)
        {
            int count = 0;
            var list = new DDmanageBll().Query();

            if (sid != 0)
            {
                list = list.Where(d => d.DDstatus_id == sid);
            }

            count = list.Count();

            var data = list.OrderBy(d => d.id).Skip((page - 1) * limit).Take(limit);



            var zhi = from r in data
                      select new DDmanageDto
                      {
                          id = r.id,
                          quid = r.quid,
                          multi_store = r.multi_store,
                          created_time = r.created_time,
                          address = r.address,
                          phonenumber = r.phonenumber,
                          user_name = r.user_name,
                          amount = r.amount,
                          pay_mode = r.pay_mode,
                          DDstatus_id = r.DDstatus_id,
                          status_text = r.order_status.status_text,
                          postscript = r.postscript
                      };


            ResultPageList<DDmanageDto> result = new ResultPageList<DDmanageDto>();
            result.code = count > 0 ? 1 : 0; ;
            result.msg = "ok";
            result.count = count;
            result.data = zhi.ToList();

            return result;
        }

        [Route("Api/PorderLast")]
        public ResultData<DDmanageDto> GetLast()
        {
            int count = 0;
            var list = new DDmanageBll().Query();
            var last = list.OrderByDescending(s => s.id).FirstOrDefault();
            DDmanageDto dto=new DDmanageDto();
            dto.id = last.id;
            dto.quid=last.quid;
            dto.multi_store= last.multi_store;
            dto.created_time = last.created_time;
            dto.address = last.address;
            dto.phonenumber = last.phonenumber;
            dto.user_name = last.user_name;
            dto.amount = last.amount;
            dto.pay_mode = last.pay_mode;
            dto.DDstatus_id = last.DDstatus_id;
            dto.postscript = last.postscript;
            dto.status_id = last.order_status.status_id;
            dto.status_text = last.order_status.status_text;


            ResultData<DDmanageDto> result = new ResultData<DDmanageDto>();
            result.code = count;
            result.msg = "ok";
            result.data = dto;

            return result;
        }

        [Route("Api/DDmanage/Status")]
        public ResultData<List<order_statusDto>> GetStatus()
        {

            var list = model.order_status.ToList();

            var data = from r in list
                       select new order_statusDto
                       {
                           status_id = r.status_id,
                           status_text = r.status_text,

                       };

            ResultData<List<order_statusDto>> result = new ResultData<List<order_statusDto>>();
            result.code = 0;
            result.msg = "ok";
            result.data = data.ToList();

            return result;
        }

        // GET: api/DDmanage/5
        public string Get(int id)
        {
            return "value";
        }

        [Route("Api/DDmanage/CartZhan")]
        public Result PostCart(int id,[FromBody] DDmanageDto value)
        {

            DDmanage dDmanage = new DDmanage();
            dDmanage.id = value.id;
            dDmanage.multi_store = value.multi_store;
            dDmanage.created_time = value.created_time;
            dDmanage.address = value.address;
            dDmanage.phonenumber = value.phonenumber;
            dDmanage.user_name = value.user_name;
            dDmanage.amount = value.amount;
            dDmanage.pay_mode = value.pay_mode;
            dDmanage.DDstatus_id = value.DDstatus_id;
            dDmanage.postscript = value.postscript;


            ObjectParameter no = new ObjectParameter("no", "");
            model.proc_common_create_flowNo("DDmanage", "quid", "NN", no);
            string ID = no.Value.ToString();
            dDmanage.quid = ID;

            bool bol = new DDmanageBll().Add(dDmanage);

            if (bol) {
                int ddid = model.DDmanage.Where(d => d.quid == ID).FirstOrDefault().id;

                List<milktea_cart_detail> list = model.milktea_cart_detail.Where(d => d.Cart_id == id).ToList();


                foreach (var item in list)
                {
                    DDmanageDetail detail = new DDmanageDetail();
                    detail.detail_id = 0;
                    detail.Dgdetail_id = ddid;
                    detail.id = (int)item.Goods_Id;
                    detail.number = item.quantity;
                    detail.price = item.price;
                    detail.props_text = item.specification;

                    model.DDmanageDetail.Add(detail);

                    model.SaveChanges();
                }

                var data = model.milktea_cart.Where(c => c.cart_id == id).FirstOrDefault();

                data.Dstatus = false;

                model.SaveChanges();

            }

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "订单生成成功！" : "订单生成失败";
            return result;
        }

        // POST: api/DDmanage
        public Result Post([FromBody] DDmanageDto value)
        {

            DDmanage dDmanage = new DDmanage();
            dDmanage.id = value.id;
            dDmanage.multi_store = value.multi_store;
            dDmanage.created_time = value.created_time;
            dDmanage.address = value.address;
            dDmanage.phonenumber = value.phonenumber;
            dDmanage.user_name = value.user_name;
            dDmanage.amount = value.amount;
            dDmanage.pay_mode = value.pay_mode;
            dDmanage.DDstatus_id = value.DDstatus_id;
            dDmanage.postscript = value.postscript;

            ObjectParameter no = new ObjectParameter("no", "");
            model.proc_common_create_flowNo("DDmanage", "quid", "NN", no);
            string ID = no.Value.ToString();
            dDmanage.quid = ID;

            bool bol = new DDmanageBll().Add(dDmanage);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "添加成功！" : "error";
            return result;
        }
        [Route("Api/DDmanage/PUT")]
        // PUT: api/DDmanage/5
        public Result Put([FromBody] DDmanageDto value)
        {
            DDmanage dDmanage = new DDmanage();
            dDmanage.id = value.id;
            dDmanage.quid=value.quid;
            dDmanage.multi_store = value.multi_store;
            dDmanage.created_time = value.created_time;
            dDmanage.address = value.address;
            dDmanage.phonenumber = value.phonenumber;
            dDmanage.user_name = value.user_name;
            dDmanage.amount = value.amount;
            dDmanage.pay_mode = value.pay_mode;
            if (value.DDstatus_id==1) {
                dDmanage.DDstatus_id = 2;
            }
            if (value.DDstatus_id == 2) {
                dDmanage.DDstatus_id = 3;
            }
            dDmanage.postscript = value.postscript;

            bool bol = new DDmanageBll().Update(dDmanage);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "修改成功！" : "出错了！";
            return result;
        }

        [Route("Api/DDmanage/UpdateStatus")]
        public Result PutStatus(int id,int sid)
        {

            var data = model.DDmanage.Where(d => d.id == id).FirstOrDefault();

            data.DDstatus_id = sid;

            int count = model.SaveChanges();

            Result result = new Result();
            result.code = count;
            result.msg = count > 0 ? "状态修改完成":"状态修改失败";

            return result;
        }

        // DELETE: api/DDmanage/5
        public Result Delete(int id)
        {
            bool bol = new DDmanageBll().DeleteDDmanage(id);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "删除成功！" : "出错了！";
            return result;
        }
        [HttpDelete]
        [Route("Api/DD/del")]
        public Result DeleteAll(int[] ids)
        {
            bool bol = new DDmanageBll().DeleteDDAll(ids);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "删除成功！" : "出错了！";
            return result;
        }
    }
}
